using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Matrix.Notification.Application.Channels;

namespace Matrix.Notification.Services
{
    /// <summary>
    /// 推送通知服务接口
    /// </summary>
    public interface IPushNotificationService
    {
        /// <summary>
        /// 发送推送通知
        /// </summary>
        /// <param name="message">推送消息</param>
        /// <param name="deviceToken">设备令牌</param>
        /// <param name="platform">推送平台</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>推送结果</returns>
        Task<PushNotificationResult> SendAsync(
            PushNotificationMessage message,
            string deviceToken,
            PushPlatform platform,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 批量发送推送通知
        /// </summary>
        /// <param name="message">推送消息</param>
        /// <param name="deviceTokens">设备令牌列表</param>
        /// <param name="platform">推送平台</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>推送结果列表</returns>
        Task<List<PushNotificationResult>> SendBatchAsync(
            PushNotificationMessage message,
            IEnumerable<string> deviceTokens,
            PushPlatform platform,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 验证设备令牌
        /// </summary>
        /// <param name="deviceToken">设备令牌</param>
        /// <param name="platform">推送平台</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>是否有效</returns>
        Task<bool> ValidateDeviceTokenAsync(
            string deviceToken,
            PushPlatform platform,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 检测推送平台
        /// </summary>
        /// <param name="deviceToken">设备令牌</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>推送平台类型</returns>
        Task<PushPlatform?> DetectPlatformAsync(
            string deviceToken,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取平台推送统计信息
        /// </summary>
        /// <param name="platform">推送平台</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>统计信息</returns>
        Task<PushNotificationStatistics> GetStatisticsAsync(
            PushPlatform? platform = null,
            DateTime? startTime = null,
            DateTime? endTime = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 检查推送服务可用性
        /// </summary>
        /// <param name="platform">推送平台</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>是否可用</returns>
        Task<bool> IsAvailableAsync(
            PushPlatform platform,
            CancellationToken cancellationToken = default);
    }

    /// <summary>
    /// 推送通知结果
    /// </summary>
    public class PushNotificationResult
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 消息ID
        /// </summary>
        public string? MessageId { get; set; }

        /// <summary>
        /// 设备令牌
        /// </summary>
        public string DeviceToken { get; set; } = string.Empty;

        /// <summary>
        /// 推送平台
        /// </summary>
        public PushPlatform Platform { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string? ErrorMessage { get; set; }

        /// <summary>
        /// 错误代码
        /// </summary>
        public string? ErrorCode { get; set; }

        /// <summary>
        /// 推送时间
        /// </summary>
        public DateTime SentTime { get; set; }

        /// <summary>
        /// 额外信息
        /// </summary>
        public Dictionary<string, object> Metadata { get; set; } = new();
    }

    /// <summary>
    /// 推送通知统计信息
    /// </summary>
    public class PushNotificationStatistics
    {
        /// <summary>
        /// 推送平台
        /// </summary>
        public PushPlatform? Platform { get; set; }

        /// <summary>
        /// 总发送数
        /// </summary>
        public long TotalSent { get; set; }

        /// <summary>
        /// 成功数
        /// </summary>
        public long SuccessCount { get; set; }

        /// <summary>
        /// 失败数
        /// </summary>
        public long FailureCount { get; set; }

        /// <summary>
        /// 成功率
        /// </summary>
        public double SuccessRate => TotalSent > 0 ? (double)SuccessCount / TotalSent : 0;

        /// <summary>
        /// 统计开始时间
        /// </summary>
        public DateTime StartTime { get; set; }

        /// <summary>
        /// 统计结束时间
        /// </summary>
        public DateTime EndTime { get; set; }

        /// <summary>
        /// 按错误类型分组的统计
        /// </summary>
        public Dictionary<string, long> ErrorBreakdown { get; set; } = new();

        /// <summary>
        /// 平均推送时间（毫秒）
        /// </summary>
        public double AveragePushTimeMs { get; set; }
    }
}